﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class Quanlyuserduoiquyen : UserControl
    {
        public Quanlyuserduoiquyen()
        {
            InitializeComponent();
        }

        private void Quanlyuserduoiquyen_Load(object sender, EventArgs e)
        {
            dataGridViewX1.DataSource = UserBUS.GetUserDuoiQuyen(StaticVariables.UserPriority);
        }

        private void dataGridViewX1_SelectionChanged(object sender, EventArgs e)
        {
            try{
                tbID.Text = dataGridViewX1.SelectedRows[0].Cells[0].Value.ToString();
                tbPw.Text = dataGridViewX1.SelectedRows[0].Cells[1].Value.ToString();
                tbQuyen.Text = dataGridViewX1.SelectedRows[0].Cells[3].Value.ToString();
                tbRole.Text = dataGridViewX1.SelectedRows[0].Cells[2].Value.ToString();
            }
            catch(Exception ex){

            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            UserDTO user = new UserDTO();
            try
            {
                user.ID = tbID.Text;
                user.Password = tbPw.Text;
                user.Quyen = int.Parse(tbQuyen.Text);
                user.VaiTro = tbRole.Text;
                if (UserBUS.InsertUser(user))
                {
                    lblThongbao.Text = "Them thanh cong";
                    dataGridViewX1.DataSource = UserBUS.GetUserDuoiQuyen(StaticVariables.UserPriority);
                }
                else{
                    lblThongbao.Text = "Them that bai! Vui long kiem tra lai";
                }

            }catch(Exception ex)
            {

            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            UserDTO user = new UserDTO();
            try
            {
                user.ID = tbID.Text;
                user.Password = tbPw.Text;
                user.Quyen = int.Parse(tbQuyen.Text);
                user.VaiTro = tbRole.Text;
                if (UserBUS.UpdateUser(user))
                {
                    lblThongbao.Text = "Luu thanh cong";
                    dataGridViewX1.DataSource = UserBUS.GetUserDuoiQuyen(StaticVariables.UserPriority);
                }
                else
                {
                    lblThongbao.Text = "Luu that bai! Vui long kiem tra lai";
                }

            }
            catch (Exception ex)
            {

            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (UserBUS.DeleteUser(tbID.Text))
            {
                lblThongbao.Text = "Xoa thanh cong!";
            }
            else lblThongbao.Text = "Xoa that bai!";
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            tbID.Text = "";
            tbPw.Text = "";
            tbQuyen.Text = "";
            tbRole.Text = "";
        }


    }
}
